#include "utils.h"

#include <stdio.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <malloc.h>

Random g_rand;

double mtime()
{
#ifdef _WIN32
    return double(timeGetTime())/1000.0;
#else
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return double(tv.tv_sec) + double(tv.tv_usec)/1000000.0;
#endif
}

GLuint compileShader(GLenum shaderType, const char *src, int srcSize)
{
  GLuint shader = glCreateShader(shaderType);
  glShaderSource(shader, 1, &src, &srcSize);
  assert(glGetError() == 0);
  glCompileShader(shader);
  printf("%s shader compiler output:\n", shaderType==GL_VERTEX_SHADER?"Vertex":"Fragment");
  printInfoLog(shader);
  assert(glGetError() == 0);
  return shader;
}

void printInfoLog(GLuint handle)
{
  GLint logLength;
  glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logLength);

  char *log = (char*)alloca(logLength+1);
  log[logLength] = '\0';

  glGetShaderInfoLog(handle, logLength, NULL, log);
  if(logLength)
  {
    printf("%s\n", log);
  }
}
